/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.kademelia.model;

import java.math.BigInteger;

import org.kad.gwt.web.client.kademelia.KadProtocol;

/**
 * A Kademlia identifier.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
public class KadId {
    /**
     * serial uid.
     */
    private static final long   serialVersionUID = -1947301454292044365L;
    /**
     * All hexa chars.
     */
    private static final char[] CHARS            = "0123456789ABCDEF".toCharArray();

    /**
     * 
     */
    private String              value;

    /**
     * 
     */
    public KadId() {
    }

    /**
     * @param val
     */
    public KadId(String val) {
        this.value = val.toUpperCase();
    }

    /**
     * Constructor by copy.
     * 
     * @param idIn
     *            The id to copy
     */
    public KadId(KadId idIn) {
        this(idIn.value);
    }

    public BigInteger distance(KadId id) {
        return asBigInteger().xor(id.asBigInteger());
    }

    /**
     * @return
     */
    private BigInteger asBigInteger() {
        return new BigInteger(value, 16);
    }

    /*
     * (non-Javadoc)
     * @see java.math.BigInteger#toString()
     */
    @Override
    public String toString() {
        return value;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        KadId other = (KadId) obj;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @return A random id
     */
    public static KadId generateOne() {
        char[] uuid = new char[KadProtocol.ID_LENGTH / CHARS.length];
        for (int i = 0; i < uuid.length; i++) {
            uuid[i] = CHARS[(int) (Math.random() * CHARS.length)];
        }
        return new KadId(new String(uuid));
    }
}
